package com.swsc.xapp.baseContent;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.swsc.xapp.R;
import com.swsc.xapp.activity.mainActivity.MainActivity;

import com.swsc.xapp.utils.KeepAliveUtils;
import com.swsc.xapp.utils.NativeJSUtils;
import com.swsc.xapp.utils.NotificationUtils;
import com.swsc.xapp.utils.httpUtil.HttpRequestUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 启动页
 */
public class SplashActivity extends BaseActivity implements HttpRequestUtils.MyNetCall {

    private static final int splashSHowTime = 1000 * 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFullScreen(true);
        setStatusBar(true);
        setStatusIconAndTextColor("black");
        setContentView(R.layout.activity_splash);
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;     // 屏幕宽度（像素）
        ImageView iv = (ImageView)findViewById(R.id.activity_splash_iv);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) iv.getLayoutParams();
        layoutParams.width = width;
        layoutParams.height = (int) (width * 1.7718);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // 电池优化白名单
        if(!KeepAliveUtils.isIgnoringBatteryOptimizations(this)){
            KeepAliveUtils.requestIgnoreBatteryOptimizations(this);
        }

        //提示: 需要与服务共享数据自行绑定服务
        // 安卓引导用户设置APP运行后台保活4.开启通知使用权限(有的手机需要开启才能重启进程)
        if (!NotificationUtils.isNotificationListenerEnabled(this)) {
//            NotificationUtils.openNotificationListenSettings(this);
        }

        if (Build.VERSION.SDK_INT > 22) {
            // 检查权限
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
            } else {
                handler.sendEmptyMessageDelayed(1, splashSHowTime);
            }
        } else {
            handler.sendEmptyMessageDelayed(1, splashSHowTime);
        }
        // 初始化
        NativeJSUtils.initBaseUrl();
    }

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    SplashActivity.this.finish();
                    break;
                case 2:
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100 /*&& grantResults[0] == PackageManager.PERMISSION_GRANTED*/) {
            handler.sendEmptyMessageDelayed(1, splashSHowTime);
        }
    }

    @Override
    public void success(Call call, Response response) throws IOException {
        //TODO
    }

    @Override
    public void failed(Call call, IOException e) {

    }



}
